//
//  SandBoxStore.m
//  UI17_本地存储(sendbox)
//
//  Created by 宝龙 on 16/5/25.
//  Copyright © 2016年 宝龙. All rights reserved.
//

#import "SandBoxStore.h"

@implementation SandBoxStore


+ (instancetype)sharedSandBoxStore {
    static SandBoxStore *store = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        store = [[SandBoxStore alloc] init];
    });

    return  store;
}


#pragma mark - public
- (NSString *)pathForSandBox {
    
    return NSHomeDirectory();
}

- (NSString *)pathForDocumentDir {
    
    return  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

}

- (NSString *)pathForLibrary {
    return  [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, NO) lastObject];
}

- (NSString *)pathForCache {
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSString *)pathForTmp {

    return NSTemporaryDirectory();
}

- (NSString *)pathForApp {

    return [[NSBundle mainBundle] bundlePath];
}

- (BOOL)storeFileWithContent:(id)obj Forkey:(NSString *)key {

    if([obj isKindOfClass:[NSArray class]]) {
        NSLog(@"数组");
       return [self storeFileWithArray:obj ForKey:key];
    } else if ([obj isKindOfClass:[NSDictionary class]]) {
        NSLog(@"字典");
       return [self storeFileWithDic:obj ForKey:key];
    }else if ([obj isKindOfClass:[UIImage class]]) {
        NSLog(@"图片");
       return [self storeFileWithImage:obj forkey:key];
    }else if([obj isKindOfClass:[NSString class]]){
        NSLog(@"字符串");
       return [self storeFileWithString:obj ForKey:key];
    }else if([obj isKindOfClass:[NSData class]]){
        NSLog(@"Data");
        return [self storeFileWithData:obj ForKey:key];
    }else {
        NSLog(@"其他复杂对象");
        return [self storeByArchiverWithObject:obj ForKey:key];
    }
    
}



- (id)readFileForkey:(NSString *)key {


    id result = nil;
    
    result = [self readFileImageForkey:key];
    if (result) {
        NSLog(@"取回图片%@",result);
        return result;
    }
    
    result = [self readFileWithArrayforKey:key];
    if (result) {
        NSLog(@"取回数组%@",result);
        return result;
    }
    
    result = [self readFileWithDicForKey:key];
    if (result) {
        if ([[result valueForKey:@"$archiver"] isEqualToString:@"NSKeyedArchiver"]) {
            NSLog(@"取回复杂对象%@",result);
            return [self readByUnArchiverWithkey:key];
        }else {
            NSLog(@"取回字典:%@",[result valueForKey:@"$archiver"]);
            return result;
        }
    }
    
    result = [self readFileWithStringForkey:key];
    if (![(NSString *)result isEqualToString:@""]) {
        NSLog(@"取回字符串==%@==",result);
        return result;
    }
    
    
    result = [self readFileDataForkey:key];
    if (result) {
        NSLog(@"取回data%@", result );

        return result;
    }
    
   


    
    return nil;
}


/** 字符串存取*/
- (BOOL)storeFileWithString:(NSString *)content ForKey:(NSString *) key {

return [content writeToFile:[[self pathForDocumentDir] stringByAppendingPathComponent:key] atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

- (NSString *)readFileWithStringForkey:(NSString *)key {
    NSString *pathOfFile = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    NSLog(@"%@",pathOfFile);
   return [NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];
}


/** 数组,字典存取*/
- (BOOL)storeFileWithArray:(NSArray *)content ForKey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    return [content writeToFile:path atomically:YES];
}


- (NSArray *)readFileWithArrayforKey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    
    return  [NSArray arrayWithContentsOfFile:path];

}

- (BOOL) storeFileWithDic:(NSDictionary *)content ForKey:(NSString *)key {

    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    return  [content writeToFile:path atomically:YES];
}

- (NSDictionary *)readFileWithDicForKey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    
    return  [NSDictionary dictionaryWithContentsOfFile:path];
    
}





/** 读取data类型*/
- (BOOL)storeFileWithData:(NSData *)content ForKey:(NSString *) key{
   return [content writeToFile:[[self pathForDocumentDir] stringByAppendingPathComponent:key] atomically:YES];
}

- (NSData *)readFileDataForkey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];    
    return [NSData dataWithContentsOfFile:path];
}


- (BOOL)storeFileToCacheWithData:(NSData *)content ForKey:(NSString *) key{
    
    return [content writeToFile:[[self pathForCache] stringByAppendingPathComponent:key] atomically:YES];
}

- (NSData *)readFileInCacheWithDataForkey:(NSString *)key {
    NSString *path = [[self pathForCache] stringByAppendingPathComponent:key];
    return [NSData dataWithContentsOfFile:path];
}


/** 图片存取*/
- (BOOL)storeFileWithImage:(UIImage *)content forkey:(NSString *)key {
    NSData *data;
    if (!UIImageJPEGRepresentation(content, 1.0f)) {
        data = UIImagePNGRepresentation(content);
    }else {
        
        data = UIImageJPEGRepresentation(content, 1.0f);
    }
    return  [self storeFileWithData:data ForKey:key];
}


- (UIImage *)readFileImageForkey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    return [UIImage imageWithContentsOfFile:path];
}


- (BOOL)storeFileWithImageData:(NSData *)imageData Forkey:(NSString *)key {
    
    return [self storeFileWithData:imageData ForKey:key];
}

- (NSData *)readFileImageDataForkey:(NSString *)key {
    NSString *path = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    return [NSData dataWithContentsOfFile:path];
}


/** 归档反归档*/
- (BOOL)storeByArchiverWithObject:(id)obj ForKey:(NSString *)key {
    
    NSMutableData *data = [NSMutableData data];
    //归档过程
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    [archiver encodeObject:obj forKey:key];
    
    [archiver finishEncoding];
    
    //data储存
    
    return  [self storeFileWithData:data ForKey:key];

}


- (id)readByUnArchiverWithkey:(NSString *)key {

    NSString *path  = [[self pathForDocumentDir] stringByAppendingPathComponent:key];
    
    //读取数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    
    //返规当
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
    id result = [unArchiver decodeObjectForKey:key];
    
    [unArchiver finishDecoding];
    
    return result;
    
}


@end
